package com.mes.check.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.mes.check.manager.iface.IRejectManager;
import com.mes.check.dao.RejectDao;
import com.mes.check.dao.iface.IDefectJudgeDao;
import com.mes.check.dao.iface.IDefectJudgeDetailDao;
import com.mes.check.dao.iface.IRejectDao;
import com.mes.check.domain.DefectJudge;
import com.mes.check.domain.DefectJudgeDetail;
import com.mes.check.domain.Reject;
import com.mes.common.domain.BasicModel;
import com.mes.common.manager.BasicManager;
import com.mes.common.util.CodeUtil;
import com.mes.common.util.SysUtil;
import com.mes.task.domain.OperationTask;
import com.mes.task.domain.WorkRecord;
import com.mes.task.manager.iface.IOperationTaskManager;

@Component
public class RejectManager extends BasicManager<Reject> implements IRejectManager {
	private final static Log log = LogFactory.getLog(RejectManager.class);

	@Autowired
	private CodeUtil codeUtil;
	@Autowired
	private IRejectDao rejectDao;
	@Autowired
	private IDefectJudgeDao defectJudgeDao;
	@Autowired
	private IDefectJudgeDetailDao defectJudgeDetailDao;
	@Autowired
	IOperationTaskManager operationTaskManager;

	/**
	 * 将报废单设置为有效或者无效状态（设置有效性同时，反馈检验工序的完成进度）
	 * 
	 * @param rejectUid
	 * @throws BaseBusinessException
	 */
	// @Transactional
	@Override
	public void changeRejectBillValidity(String rejectUid, Boolean effective) throws Exception {
		try {
			Reject rejectBill = this.getById(rejectUid);
			rejectBill.setIsInEffect(effective);
			this.update(rejectBill);

			float rejectQty = (float) rejectBill.getRejectQty().doubleValue();
			if (effective == false) {
				rejectQty = 0 - rejectQty;

				List<DefectJudgeDetail> list = defectJudgeDetailDao
						.getDefectJudgeDetailByRelationBillUid(rejectBill.getRejectUid());
				for (DefectJudgeDetail defectDetail : list) {
					defectDetail.setRelationBillUid(null);
					defectDetail.setRelationBillId(null);
					defectJudgeDetailDao.update(defectDetail);
				}
			}

			String taskuid = rejectBill.getTaskUid();
			OperationTask operationTask = operationTaskManager.getById(taskuid);

			if ("202".endsWith(operationTask.getTaskType())) { // 如果是检验工序，则反馈完成进度
				List workRecList = new ArrayList(10);
				// 对反馈信息进行初始化；
				WorkRecord workRec = new WorkRecord();
				workRec.setTaskUid(taskuid);
				String sessionEmployeeId = SysUtil.getSessionEmployeeUid();
				workRec.setWorkerUid(sessionEmployeeId);
				// 获取更改的当前时间；
				java.util.Date now = new java.util.Date();
				java.sql.Timestamp time = new java.sql.Timestamp(now.getTime());
				workRec.setCreateTime(time);
				// 设置加工日期
				Date workDate = now;
				java.sql.Timestamp date = new java.sql.Timestamp(workDate.getTime());
				workRec.setWorkDate(date);
				workRec.setCompleteQty(rejectQty);
				workRec.setCreatorUid(sessionEmployeeId);
				// workType＝1代表正常的加工记录；
				workRec.setWorkType(1);
				workRec.setObjectState(BasicModel.OBJECTSTATE_TOBEADDED); // 新增一条记录
				workRecList.add(workRec);

				operationTaskManager.feedbackTask(workRecList);
			}

			// Slog slog = new Slog();
			// slog.setUuid(IdentityGenerator.GetIdentityString());
			// slog.setLogUid(slog.getUuid());
			// slog.setObjectState(1);
			// slog.setEmployeeUid(MMUtils.getSessionEmployeeId());
			// slog.setLogTime(new Date());
			// slog.setLogType("9");
			// slog.setParaValue("执行RejectManagerImpl的changeRejectBillValidity方法，下面开始更新工序的报废数量");
			// SlogManager slogManager = SpringUtils.getBean("slogManager");
			// slogManager.save(slog);

			operationTaskManager.updateRejectQty(taskuid, rejectQty);

		} catch (Exception e) {
			log.info(e.getStackTrace());
			throw e;
		}
	}

	public void CreateRejectBill(String defectJudgeBillUid) throws Exception {
		log.info("不合格品审理单唯一号" + defectJudgeBillUid);
		float rejectnumber = 0;
		try {
			DefectJudge defectJudge = (DefectJudge) defectJudgeDao.getById(defectJudgeBillUid);
			List<DefectJudgeDetail> defectJudgeDetailList = (List<DefectJudgeDetail>) defectJudgeDetailDao
					.findByProperty("defectJudgeUid", defectJudgeBillUid);
			if (defectJudgeDetailList.size() > 0) {
				for (DefectJudgeDetail tempDetail : defectJudgeDetailList) {

					if (tempDetail.getJudgeResult().equals("8")) {
						rejectnumber = rejectnumber + tempDetail.getDefectQty();
					}
				}
				if (rejectnumber > 0) {
					String rejectid = SysUtil.getUUID();
					String billnumber = codeUtil.getRejectBillNumber();
					
					Reject reject = new Reject();
					reject.setUuid("BF" + rejectid);
					reject.setRejectId(rejectid);
					reject.setBillNumber(billnumber);
					reject.setDefectJudgeUid(defectJudgeBillUid);
					reject.setTaskUid(defectJudge.getTaskUid());
					reject.setDeptUid(defectJudge.getMasterShop().getParentDeptUid());
					reject.setRejectBillState(Reject.REJECT_BILLSTATE_UNCLOSED);
					reject.setIsInEffect(true);
					reject.setCheckerUid(defectJudge.getCheckerUid());
					reject.setCreatorUid(defectJudge.getCreatorUid());
					reject.setCheckTime(defectJudge.getCheckTime());
					reject.setRejectQty(rejectnumber);
					this.add(reject);
					log.info("报废单号为" + billnumber);
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}


}
